<template>
  <div class="center">
    <page-layout>
      <a-row :gutter="[15, 15]">
        <a-col :span="6">
          <a-card>
            <a-avatar class="photo" :size="64" v-bind:src="form.photo"/>
            <div class="account">{{ form.account }}</div>
            <!-- <div class="address">{{form.region}}</div> -->
            <a-divider />
            <div class="synopsis">{{ form.synopsis }}</div>
          </a-card>
        </a-col>
        <a-col :span="18">
          <a-card>
            <a-tabs @change="callback">
              <a-tab-pane key="1" tab="基本信息">
                <a-form
                  ref="ruleForm"
                  :model="form"
                  :rules="rules"
                  :label-col="labelCol"
                  :wrapper-col="wrapperCol"
                  style="margin-top: 20px"
                >
                  <a-form-item ref="account" label="账号" name="account">
                    <a-input v-bind:value="account" disabled="true" />
                  </a-form-item>
                  <a-form-item label="昵称" name="nickname">
                    <a-textarea v-model:value="form.nickname" />
                  </a-form-item>
                  <a-form-item label="性别" name="gender">
                    <a-radio-group
                      v-model:value="form.gender"
                      button-style="solid"
                    >
                      <a-radio-button value="0"> 男 </a-radio-button>
                      <a-radio-button value="1"> 女 </a-radio-button>
                    </a-radio-group>
                  </a-form-item>
                  <a-form-item label="简介" name="synopsis">
                   <a-textarea v-model:value="form.synopsis" />
                  </a-form-item>                  
                  <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
                    <a-button class="user-btn" type="primary" @click="onSubmit"> 保存 </a-button>
                    <a-button class="user-btn" @click="resetForm">
                      重置
                    </a-button>
                  </a-form-item>
                </a-form>
              </a-tab-pane>
              <a-tab-pane key="2" tab="修改密码" force-render>
                 <a-form
                  ref="passForm"
                  :model="passForm"
                  :rules="passRules"
                  :label-col="labelCol"
                  :wrapper-col="wrapperCol"
                  style="margin-top: 20px"
                >
                  <a-form-item required has-feedback label="旧密码" name="oldPassword">
                    <a-input type="password" v-model:value="passForm.oldPassword" autocomplete="off"/>
                  </a-form-item>
                  <a-form-item required has-feedback label="新密码" name="password">
                    <a-input type="password" v-model:value="passForm.password" autocomplete="off"/>
                  </a-form-item>
                  <a-form-item required has-feedback label="确认新密码" name="confirmPassword">
                    <a-input type="password" v-model:value="passForm.confirmPassword" autocomplete="off"/>
                  </a-form-item>
                  <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
                    <a-button class="user-btn" type="primary" @click="onSubmitPass"> 保存 </a-button>
                  </a-form-item>
                </a-form>
              </a-tab-pane>
            </a-tabs>
          </a-card>
        </a-col>
      </a-row>
    </page-layout>
  </div>
</template>

<script>
import { userProfileAPI, userModifyAPI, userChangePassAPI } from "../../api/api";
import { message } from "ant-design-vue";
import { stringify } from 'querystring';
export default {
  data() {
    let validateOldPass = async (rule, value) => {
      if (value === '') {
        return Promise.reject('请输入密码');
      } else {
        return Promise.resolve();
      }
    };
    let validateNewPass = async (rule, value, callback) => {
      if (value === '') {
        return Promise.reject('请输入新密码');
      }  else {
        return Promise.resolve();
      }
    };
    let validateConfirmPass = async (rule, value, callback) => {
      if (value === '') {
        return Promise.reject('请再次输入新密码');
      } else if (value !== this.passForm.password) {
        return Promise.reject("两次输入密码不一致");
      } else {
        return Promise.resolve();
      }
    };
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 12 },
      other: "",
      account: "",
      form: {
        nickname: "",
        gender: "0",
        synopsis: "",
        photo: "",
      },
      rules: {        
        nickname: [
          {
            required: true,
            message: "请输入昵称",
            trigger: "blur",
          },
        ],
        gender: [
          {
            required: true,
            message: "请选择性别",
            trigger: "change",
          },
        ],
        synopsis: [
          {
            required: true,
            message: "请输入简介",
            trigger: "blur",
          },
        ],
      },
      passForm: {
        oldPassword: "",
        password: "",
        confirmPassword: ""
      },
      passRules: {        
        oldPassword: [{ validator: validateOldPass, trigger: 'change' }],
        password: [{ validator: validateNewPass, trigger: 'change' }],
        confirmPassword: [{ validator: validateConfirmPass, trigger: 'change' }],
      },
    };
  },
  //在这里调用ajax请求方法
  created() {
    this.InitData();
  },
  methods: {
    InitData() {
      userProfileAPI().then((res) => {
        this.account = res.data.data.account;
        this.form.nickname=res.data.data.nickname;
        this.form.synopsis = res.data.data.synopsis;
        this.form.photo = res.data.data.photo;        
        this.form.gender = res.data.data.gender+'';
      });
    },
    onSubmit() {
      this.$refs.ruleForm
        .validate()
        .then(() => {
          var model=JSON.parse(JSON.stringify(this.form));
          model.gender=parseInt(model.gender)
          userModifyAPI(model).then((res) => {
            if (res.data.succeeded) {
              message.success("保存成功！");
            } else {
              message.error(stringify(res.data.errors));
            }
          });
          console.log("values", this.form);
        })
        .catch((error) => {
          console.log("error", error);
        });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
    onSubmitPass(){
      this.$refs.passForm
        .validate()
        .then(() => {         
          userChangePassAPI(this.passForm).then((res) => {
            if (res.data.succeeded) {
              message.success("修改成功！");
            } else {
              message.error(stringify(res.data.errors));
            }
          });
          console.log("values", this.form);
        })
        .catch((error) => {
          console.log("error", error);
        });
    }
  },
};
</script>
<style lang="less">
.center {
  .photo {
    margin-left: 36%;
    margin-top: 40px;
  }
  .account {
    margin-left: 37%;
    margin-top: 15px;
    font-size: 16px;
  }
  .address {
    margin-left: 43%;
    font-size: 14px;
    margin-top: 10px;
  }
  .synopsis {
    margin-left: 20%;
  }
}
.user-btn {
  margin: 5px;
}
</style>